package com.neusoft.neu24.his4;

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableListValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.Pane;
import javafx.util.StringConverter;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class HelloController {
    @FXML
    TableView tb_demo;



    @FXML
    ComboBox cb_demo;


    @FXML
    public ListView lv1;


    private List<String> ll = new ArrayList<>();

    // 创建ObservableList
    ObservableList<String> items = FXCollections.observableArrayList(ll);

    @FXML
    private void initialize() {


        List<Person> lps = new ArrayList<>();
        lps.add(new Person());
        lps.add(new Person());
        lps.add(new Person());
        lps.add(new Person());
        lps.add(new Person());
        lps.add(new Person());


        TableColumn<Person, Integer> column1 =
                new TableColumn<>("First Name");

        column1.setCellValueFactory(
                new PropertyValueFactory<>("id"));


        TableColumn<Person, String> column2 =
                new TableColumn<>("Last Name");

        column2.setCellValueFactory(
                new PropertyValueFactory<>("name"));


        tb_demo.getColumns().add(column1);
        tb_demo.getColumns().add(column2);

        tb_demo.getItems().addAll(lps);







        ObservableList<Person> observableList = FXCollections.observableList(lps);
        cb_demo.setItems(observableList);
        //数据转换
        cb_demo.setConverter(new StringConverter<Person>() {
            //只显示名字
            @Override
            public String toString(Person object) {
                if (object == null) {
                    return "";
                }
                String value = object.getName();
                return value;
            }

            @Override
            public Person fromString(String string) {
                return null;
            }
        });






        ll.add(System.currentTimeMillis() + " ");
        // 创建ListView并将其items属性绑定到ObservableList
        lv1.setItems(items);
    }

    @FXML
    public   void   cb_demo_action(    ){
        int selectedIndex = cb_demo.getSelectionModel().getSelectedIndex();
        Object selectedItem = cb_demo.getSelectionModel().getSelectedItem();

        System.out.println("Selection made: [" + selectedIndex + "] " + selectedItem);
        System.out.println("   ComboBox.getValue(): " + cb_demo.getValue());
//        String value = (String) cb_demo.getValue();
//        System.out.println(value);
    }
@FXML
    public  void btn1_click(){

    }



//    private DoubleProperty prefWidthProperty ;// = pane.prefWidthProperty();
//    private SimpleStringProperty  ssp =new SimpleStringProperty();
//
//    @FXML
//    private TextField tf_width;
//
//    @FXML
//    private  TextField tf_width1;
//
//    @FXML
//    private Pane pane;
//
//    @FXML
//    private void initialize() {
//
//        prefWidthProperty =  pane.prefHeightProperty();
//
//        tf_width.textProperty().bindBidirectional(ssp);
//        tf_width1.textProperty().bindBidirectional(ssp);
//
//
//    }
//
//
//    @FXML
//    private Label welcomeText;
//
//    @FXML
//    protected void onHelloButtonClick() {
//        prefWidthProperty.setValue(200);
//        welcomeText.setText("Welcome to JavaFX Application!");
//    }
}

@Data
class Person {
    private static int index = 1;
    private int id = 1;
    private String name = "nuesoft";

    public Person() {
        id = index++;
        name += id;
    }
}